package com.sit.estatemanagementserver.controller;

import com.sit.estatemanagementserver.enity.Facility;
import com.sit.estatemanagementserver.enity.FacilityRental;
import com.sit.estatemanagementserver.enity.Response;
import com.sit.estatemanagementserver.service.FacilityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
// 重构: 将两个相关但不同的资源分开管理
public class FacilityController {
    @Autowired
    private FacilityService facilityService;

    // --- Facility Rental Endpoints ---
    @PostMapping("/facility-rentals/add")
    public Response<?> addFacilityRental(@RequestBody FacilityRental facilityRental) {
        // 直接返回Service层的处理结果
        return facilityService.addFacilityRental(facilityRental);
    }

    // --- Facility Endpoints ---
    @GetMapping("/facilities")
    public Response<List<Facility>> getFacilities() {
        List<Facility> facilities = facilityService.getAllFacilities();
        // 重构: 即使为空也返回成功的响应，数据为空列表是正常情况
        return Response.success(facilities);
    }

    @PostMapping("/facilities")
    public Response<?> addFacility(@RequestBody Facility facility) {
        if (facilityService.addFacility(facility)) {
            return Response.success();
        } else {
            return Response.error("添加设施失败");
        }
    }

    @GetMapping("/facilities/{facilityId}")
    public Response<Facility> getFacility(@PathVariable String facilityId) {
        Facility facility = facilityService.getFacilityById(facilityId);
        if (facility == null) {
            return Response.error("该设施不存在");
        } else {
            return Response.success(facility);
        }
    }

    // 重构: 使用PUT方法进行更新操作，更符合REST规范
    @PutMapping("/facilities")
    public Response<?> updateFacility(@RequestBody Facility facility) {
        if (facilityService.updateFacility(facility)) {
            return Response.success();
        } else {
            return Response.error("修改设施失败");
        }
    }

    @DeleteMapping("/facilities/{facilityId}")
    public Response<?> deleteFacility(@PathVariable String facilityId) {
        if (facilityService.deleteFacility(facilityId)) {
            return Response.success();
        } else {
            return Response.error("删除设施失败");
        }
    }
}